package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.service.CategoryService;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by lenovo on 2017/11/20.
 */
public class AdminCategory {
    CategoryService service=new CategoryServiceImpl();
    //一级菜单显示
    @URLMapping(url = "/adminCategory")
    public String categoryList(HttpServletRequest request, HttpServletResponse response){
        List<Category> list=service.findAll();
        request.setAttribute("lists",list);
        return "/admin/one/onelist.jsp";
    }
    //添加页面显示
    @URLMapping(url = "/addUICaterory")
    public String addUICaterory(HttpServletRequest request,HttpServletResponse response){
        return "Redirect:"+request.getContextPath()+"/admin/one/add.jsp";
    }
    //添加
    @URLMapping(url = "/addCategory")
    public String addCategory(HttpServletRequest request,HttpServletResponse response){
        String cname=request.getParameter("cname");
        int count=service.addCategory(cname);
        return "Redirect:"+request.getContextPath()+"/adminCategory";
    }
    //删除
    @URLMapping(url = "/deleteCategory")
    public String deleteCategory(HttpServletRequest request,HttpServletResponse response){
        String cid=request.getParameter("cid");
        int count=service.deleteCategory(cid);
        return "Redirect:"+request.getContextPath()+"/adminCategory";
    }
    //回显
    @URLMapping(url = "/editUICategory")
    public String editUICategory(HttpServletRequest request,HttpServletResponse response){
        String cid=request.getParameter("cid");
        Category category=service.findByCid(cid);
        request.setAttribute("c",category);
        return "/admin/one/edit.jsp";
    }
    //编辑
    @URLMapping(url = "/editCategory")
    public String editCategory(HttpServletRequest request,HttpServletResponse response){
        Category category=new Category();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            int count=service.editCategory(category);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/adminCategory";
    }
}
